package com.ug.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ug.common.constant.SecurityConstants;
import com.ug.common.response.ResponseMessage;
import com.ug.common.response.ResponseMessageFactory;
import com.ug.common.utils.StringUtil;
import com.ug.logs.annotation.Log;
import com.ug.system.entity.SysRole;
import com.ug.system.entity.dto.QueryDto;
import com.ug.system.entity.dto.SysRoleDto;
import com.ug.system.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author LY
 * @create 2024/7/17 10:04
 * @Description 角色控制层
 */
@Tag(name = "SysRoleController", description = "角色管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService roleService;

    /**
     * 获取角色列表
     */
    @Operation(summary = "角色列表", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("获取角色列表")
    @GetMapping("/table")
    public ResponseMessage<IPage<SysRole>> getRoleList(QueryDto queryDto, SysRoleDto sysRoleDto){
        return ResponseMessageFactory.getSuccessResponseMessage(roleService.getRoleList(queryDto, sysRoleDto));
    }

    /**
     * 获取角色列表
     * @param role 请求角色入参
     */
    @Operation(summary = "编辑角色", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("编辑角色")
    @PostMapping("/edit")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResponseMessage<String> editRole(@RequestBody SysRole role){
        String tag = StringUtil.getEditType(role.getRoleId());
        roleService.editRole(role);
        return ResponseMessageFactory.getSuccessResponseMessage(tag);
    }

    /**
     * 删除角色
     * @param roleId 请求角色id入参
     */
    @Operation(summary = "删除角色", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("删除角色")
    @DeleteMapping("/del")
    public ResponseMessage<String> delRole(@Parameter(description = "角色id") @RequestParam("roleId") Long roleId){
        roleService.delRole(roleId);
        return ResponseMessageFactory.getSuccessResponseMessage("删除成功");
    }
}
